﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using ZLDC.Common.Estate;

namespace ZLDC.DAL.Estate
{
                public class LookTelLogDAL
    {
                                                                                                        public static void p_LookTelLog_insert(int housingId, int deptId, string dept, int lookerId, string looker, string lookerPost, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@housingId", housingId));
            parms.Add(new SqlParameter("@deptId", deptId));
            parms.Add(new SqlParameter("@dept", dept));
            parms.Add(new SqlParameter("@lookerId", lookerId));
            parms.Add(new SqlParameter("@looker", looker));
            parms.Add(new SqlParameter("@lookerPost", lookerPost));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);

            db.execute_procedure("p_LookTelLog_insert", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

        }

                                                                public static int LookTelCount(int housingId, int userid, int typeid, _Database db)
        {
            int rows = 0;

            try
            {
                string sql = "";
                if (typeid == 1)
                    sql = "select COUNT(1)as total from t_LookTelLog where housingId = " + housingId + " and DATEDIFF(dd, lookdate, getdate()) = 0";
                else if (typeid == 2)
                    sql = "select COUNT(1)as total from t_LookTelLog where lookerId = " + userid + " and DATEDIFF(dd, lookdate, getdate()) = 0";

                DataSet ds = db.Query(sql);
                if (ds != null && ds.Tables.Count > 0)
                    rows = _Convert.ToInt(ds.Tables[0].Rows[0]["total"].ToString(), 0);
            }
            catch { }
            finally { db.close(); }

            return rows;
        }

                                                        public static DataTable p_LookTelLog_list_table(int housingId, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@housingId", housingId));
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            DataTable dt = null;
            dt = db.GetDataTable("p_LookTelLog_list", parms);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return dt;
        }

    }
}
